package com.opennews.openplatform


import com.opennews.openplatform.dto.IncomeExpenseDetailQueryArgument
import groovy.transform.CompileStatic
import org.springframework.http.HttpStatus

@CompileStatic
class IncomeExpenseDetailController extends BaseController {
    static allowedMethods = [
        add: "POST",
        update: "POST",
        delete: "POST"
    ]

    IncomeExpenseDetailService incomeExpenseDetailService

    // region: Get actions.
    def index() {
        render("IncomeExpenseDetailController")
    }

    def queryListWithSummaryByAccountGroup() {
        def pageSize = params.int("pageSize")
        def pageIndex = params.int("pageIndex")
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailService.queryListWithSummaryByAccountGroup(accountGroupId, pageSize, pageIndex)

        sendDataResponse(result)
    }

    def queryListByAccountGroup() {
        def pageSize = params.int("pageSize")
        def pageIndex = params.int("pageIndex")
        def startDate = SharedUtil.isNullOrEmpty(params.startDate) ? null : DateHelper.parseFromLongDateTime(params.startDate.toString())
        def endDate = SharedUtil.isNullOrEmpty(params.endDate) ? null : DateHelper.parseFromLongDateTime(params.endDate.toString())
        def keyword = params.keyword.toString()
        def keywordsSearchCondition = params.keywordsSearchCondition.toString()
        def type = params.type.toString()
        def detailType = params.detailType.toString()
        def paymentType = params.paymentType.toString()
        def bankCardId = params.bankCardId.toString()
        def accountGroupId = getClaims().accountGroupId

        // Prepares the query argument.
        def queryArgument = new IncomeExpenseDetailQueryArgument(pageSize: pageSize, pageIndex: pageIndex, accountGroupId: accountGroupId, keyword: keyword, type: type,
            detailType: detailType, paymentType: paymentType, bankCardId: bankCardId, keywordsSearchCondition: keywordsSearchCondition,
            startDate: startDate, endDate: endDate)

        def result = incomeExpenseDetailService.queryListByAccountGroup(queryArgument)

        sendDataResponse(result)
    }

    def queryTotalAmountByAccountGroup() {
        def startDate = SharedUtil.isNullOrEmpty(params.startDate) ? null : DateHelper.parseFromLongDateTime(params.startDate.toString())
        def endDate = SharedUtil.isNullOrEmpty(params.endDate) ? null : DateHelper.parseFromLongDateTime(params.endDate.toString())
        def keyword = params.keyword.toString()
        def keywordsSearchCondition = params.keywordsSearchCondition.toString()
        def type = params.type.toString()
        def detailType = params.detailType.toString()
        def paymentType = params.paymentType.toString()
        def bankCardId = params.bankCardId.toString()
        def accountGroupId = getClaims().accountGroupId

        // Prepares the query argument.
        def queryArgument = new IncomeExpenseDetailQueryArgument(accountGroupId: accountGroupId, keyword: keyword, type: type, detailType: detailType, paymentType: paymentType,
            bankCardId: bankCardId, keywordsSearchCondition: keywordsSearchCondition, startDate: startDate, endDate: endDate)

        def result = incomeExpenseDetailService.queryTotalAmountByAccountGroup(queryArgument)

        sendDataResponse(result)
    }

    def queryDetails() {
        def result = incomeExpenseDetailService.queryDetails(getClaims().accountGroupId, params.id.toString())

        sendDataResponse(result)
    }

    def queryCreditCardBill() {
        def pageSize = params.int("pageSize")
        def pageIndex = params.int("pageIndex")
        def bankCardId = params.bankCardId.toString()
        def statementDate = DateHelper.parseFromLongDateTime(params.statementDate.toString())
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailService.queryCreditCardBill(accountGroupId, pageSize, pageIndex, bankCardId, statementDate)

        sendDataResponse(result)
    }

    def queryCreditCardBillAmount() {
        def bankCardId = params.bankCardId.toString()
        def statementDate = DateHelper.parseFromLongDateTime(params.statementDate.toString())
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailService.queryCreditCardBillAmount(accountGroupId, bankCardId, statementDate)

        sendDataResponse(result)
    }

    def queryCoordinates() {
        def year = params.year.toString()
        def accountGroupId = getClaims().accountGroupId
        def result = incomeExpenseDetailService.queryCoordinates(accountGroupId, year)

        sendDataResponse(result)
    }
    //endregion

    // region: Post actions.
    def add() {
        def requestBodyData = getRequestBodyData()
        def claims = getClaims()
        incomeExpenseDetailService.add(requestBodyData, claims.accountGroupId, claims.username)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def update() {
        def requestBodyData = getRequestBodyData()
        def claims = getClaims()
        incomeExpenseDetailService.update(claims.accountGroupId, claims.username, requestBodyData)

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }

    def delete() {
        def requestBodyData = getRequestBodyData()
        def id = requestBodyData.id.toString()
        def accountGroupId = getClaims().accountGroupId
        incomeExpenseDetailService.delete(accountGroupId, [id])

        sendStatusResponse(HttpStatus.NO_CONTENT)
    }
    //endregion
}